package cn.edu.scau.cmi.maintain.service;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import java.util.UUID;

import javax.imageio.ImageIO;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.scau.cmi.ema.domain.Event;

@Service("Word2ImageService")
@Transactional(timeout=300000000)
public class Word2ImageServiceImpl implements Word2ImageService {
	// 默认格式
	private static final String FORMAT_NAME = "JPG";
	@Override
	public void getImage(Event event, Font font, Integer width, Integer height, String destPath, String fileName, Color backgroundColor, Color paintColor) throws IOException {
		mkdirs(destPath);//如果没有目录就创建
		BufferedImage bufferedImage = createImage(event, font, width, height, backgroundColor, paintColor) ;
		//保存图片
		ImageIO.write(bufferedImage, FORMAT_NAME, new File(destPath + "/" + fileName));
	}
	
	@Override
	public void getImage(String contentString, Font font, Integer width, Integer height, String destPath, String fileName, Color backgroundColor, Color paintColor) throws IOException {
		mkdirs(destPath);//如果没有目录就创建
		BufferedImage bufferedImage = createImage(contentString, font, width, height, backgroundColor, paintColor) ;
		//保存图片
		ImageIO.write(bufferedImage, FORMAT_NAME, new File(destPath + "/" + fileName));
		
	}
	
	private BufferedImage createImage(Event imagePresentString, Font font, Integer width, Integer height, Color backgroundColor, Color paintColor) {
		
		BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics2D graphics2d = (Graphics2D) bufferedImage.getGraphics();
		
		int color=getRandomColor();
		
		graphics2d.setBackground(new Color(color));
		graphics2d.setPaint(new Color(~color));//绘画笔趣反色
		graphics2d.clearRect(0, 0, width, height);
		graphics2d.setFont(font);
		//g2.setPaint(Color.red);
		

//		FontRenderContext context = graphics2d.getFontRenderContext();
//		Rectangle2D rectangle2d = font.getStringBounds(event.getId(), context);
//		double x = (width - rectangle2d.getWidth()) / 2;
//		double y = (height - rectangle2d.getHeight()) / 2;
//		double ascent = -rectangle2d.getY();
//		double baseY = y + ascent;
		
		
//		graphics2d.setPaint(new Color(0, 0, 0, 64));//阴影颜色
//		graphics2d.drawString(txt, x, y);//先绘制阴影
//		graphics2d.setPaint(Color.BLACK);//正文颜色
//		graphics2d.drawString(txt, x, y);//用正文颜色覆盖上去

		graphics2d.drawString("EMA事件的Id是： "+imagePresentString.getId(), 20, 200);
		
		if(imagePresentString.getName().length()>0) {
			if(imagePresentString.getName().length()<12) {
				String line=imagePresentString.getName().substring(0);
				graphics2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);//设置抗锯齿
				graphics2d.setPaint(new Color(0,0,0,64));
				graphics2d.drawString(line, 20, 350);
				graphics2d.setPaint(paintColor);
				graphics2d.drawString(line, 20, 350);
			}else {
				String line=imagePresentString.getName().substring(0, 12);
				graphics2d.drawString(line, 20, 350);
			}
		}
		if(imagePresentString.getName().length()>12) {
			String line=imagePresentString.getName().substring(12);
			graphics2d.drawString(line, 20, 500);
		}

		return bufferedImage;
	}
	
//	创建图片，图片的内容显示传进来的字符串。
	private BufferedImage createImage(String imagePresentString, Font font, Integer width, Integer height, Color backgroundColor, Color paintColor) {
		
		
		BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics2D graphics2d = (Graphics2D) bufferedImage.getGraphics();
		
		int color=getRandomColor();
		
		graphics2d.setBackground(new Color(color));
		graphics2d.setPaint(new Color(~color));//绘画笔趣反色
		graphics2d.clearRect(0, 0, width, height);
		graphics2d.setFont(font);
		
		if(imagePresentString.length()>0) {
			if(imagePresentString.length()<10) {
				String contentString=imagePresentString.substring(0);
				graphics2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);//设置抗锯齿
//				graphics2d.setPaint(new Color(0,0,0,64));
//				graphics2d.drawString(contentString, 20, 150);
				graphics2d.setPaint(paintColor);
				graphics2d.drawString(contentString, 20, 125);
			}else {
				String contentStringFirst=imagePresentString.substring(0, 10);
				graphics2d.drawString(contentStringFirst, 20, 90);
				String contentStringSecond=imagePresentString.substring(10);
				graphics2d.drawString(contentStringSecond, 20, 160);
			}
		}
		return bufferedImage;
	}
	
	private int getRandomColor() {
		Random random = new Random();
		return 0x000000 | random.nextInt(0xffffff);
		}

	private void mkdirs(String destPath) {
		File file = new File(destPath);
		// 当文件夹不存在时，mkdirs会自动创建多层目录，区别于mkdir．(mkdir如果父目录不存在则会抛出异常)
		if (!file.exists() && !file.isDirectory()) {
			file.mkdirs();
		}
	}
	
	public static void main(String[] args) throws FileNotFoundException, IOException {
		Word2ImageServiceImpl image=new Word2ImageServiceImpl();
		Event event=new Event();
		event.setId(100);
		event.setName("华南农业大学数学与信息学院");
		image.getImage(event, new Font("仿宋",Font.PLAIN,80), 1000, 600, "E:\\java生成的测试图片", "1.jpg",Color.red, Color.green);
		System.out.println("OK");
	}
}